<template>
  <el-dialog :title="!dataForm.id ? '修改' : '上传述职报告'" :close-on-click-modal="false" width="800px" :visible.sync="visible"
    append-to-body>
    <el-container class="texBig">
      <el-header class="titleBig">
        {{hdName}}
      </el-header>
      <el-main style="max-height:60vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
          label-width="180px">
          <el-form-item label="述职报告内容" prop="szbgNr">
            <el-input type="textarea" :rows="8" show-word-limit maxlength="5000" v-model="dataForm.szbgNr"
              placeholder="述职报告内容"></el-input>
          </el-form-item>
          <el-form-item label="上传附件" prop="szbgFjdz">
            <el-upload class="upload-demo" accept=".pdf,.doc,.docx,.PDF" :headers="headers"
              action="/admin/sys-file/upload" :on-success="handleUploadSuccess" :before-upload="beforeFileUpload"
              :on-change="handleChange" :on-preview="handlePictureCardPreview" :file-list="fileList">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip texBig"> 附件格式要求：.doc , .docx , .pdf</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <span style="float:right">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
        </span>
      </el-main>
    </el-container>

    <el-dialog title="述职报告" :visible.sync="bgdialogVisible" width="90vw" top="3vh" append-to-body>
      <iframe :src="bgUrl" style="width: 100%;height:75vh"></iframe>
      <span slot="footer" class="dialog-footer">
        <el-button @click="bgdialogVisible = false">取消</el-button>
      </span>
    </el-dialog>

  </el-dialog>

</template>

<script>
  import {
    getObj,
    addObj,
    putObj
  } from '@/api/ceping/cepinghdglszbgb'
  import {getPreview} from "@/util/util";
  import store from '@/store'

  export default {
    data() {
      return {
        hdName: '',
        visible: false,
        canSubmit: false,
        bgdialogVisible: false,
        bgUrl: "",
        dataForm: {
          id: 0,
          cpgzBpfdx: '',
          bcpDeptName: '',
          bcpUserName: '',
          szbgNr: '',
          szbgFjmc: '',
          szbgFjlx: '',
          szbgFjdz: '',
          szbgPdfdz: '',

        },
        dataRule: {
          szbgNr: [{
            required: true,
            message: '述职报告内容不能为空',
            trigger: 'blur'
          }],
          szbgFjdz: [{
            required: true,
            message: '附件不能为空',
            trigger: 'blur'
          }],
        },
        fileList: [],
        headers: {
          Authorization: 'Bearer ' + store.getters.access_token
        },
      }
    },
    methods: {
      init(id, hdName) {
        this.dataForm.id = id || 0;
        if (hdName) {
          this.hdName = hdName;
        }
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.fileList = [];
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
              if (this.dataForm.szbgFjdz) {
                this.fileList.push({
                  name: this.dataForm.szbgFjmc,
                  url: this.dataForm.szbgFjdz
                })
              }

            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshSubList')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      },
      /**
       * 文件修改
       */
      handleChange(file, fileList) {
        this.fileList = [];
        this.fileList.push(file);
      },
      /**
       * 文件上传前
       */
      beforeFileUpload(file) {
        const isType = file.type === 'application/pdf' || file.type === 'application/msword' || file.type ===
          'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        if (!isType) {
          this.$message.closeAll();
          this.$message.error('上传文件格式不对!')
          return false;
        }
        return isType;
      },
      /**
       * 文件上传成功
       */
      handleUploadSuccess(response, file, fileList) {
        if (response.code == 0) {
          this.dataForm.szbgFjdz = response.data.url;
          this.dataForm.szbgFjmc = file.name;
          this.dataForm.szbgFjlx = file.raw.type
          this.$message.closeAll();
          this.$message.success('上传成功')
        } else {
          this.$message.closeAll();
          this.$message.error('上传出错：' + response.msg)
        }
      },
      /**
       * 文件预览预留
       */
      handlePictureCardPreview(file) {
        this.bgUrl = getPreview(file.url?file.url:file.response.data.url) + "&officePreviewType=pdf";
        this.bgdialogVisible = true;
      },

    }
  }

</script>
